package br.sales.view.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.primefaces.context.RequestContext;

import br.sales.controller.IUserEntityController;
import br.sales.controller.UserEntityControllerImpl;
import br.sales.entity.UserEntity;
import br.sales.entity.exception.EntityNotExistsException;
import br.sales.persistence.exception.DBContainerNotInitializedException;
import br.sales.util.AppUtil;
import br.sales.view.util.ViewUtil;


@ViewScoped
@ManagedBean(name="userBean")
public class UserBean implements Serializable{

	private static final long serialVersionUID = 1L;
	private IUserEntityController userEntityController;
	private UserEntity userEntity;
	private String newPassword;
	private String newEmail;
	private List<UserEntity> users;
	private List<SelectItem> userTypes;
	private int userType = UserEntity.COMMON;
	private String keyword;

	public UserBean() {
		userEntityController = UserEntityControllerImpl.getInstance();
	}
	
	public void clear(boolean clearMainObject){
		if (clearMainObject){
			this.userEntity = null;
		}
		this.newPassword = "";
		this.newEmail = "";
		this.keyword = "";
		userType = UserEntity.COMMON;
	}
	
	public void search(){
		try {
			this.users = userEntityController.listByValue(keyword);
		} catch (DBContainerNotInitializedException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"database_error"), FacesMessage.SEVERITY_FATAL);
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void cancelEditUser(){
		clear(true);
	}
	
	public List<UserEntity> getAllUsers() {
		try {
			return userEntityController.listAllUsers();
		} catch (DBContainerNotInitializedException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"database_error"), FacesMessage.SEVERITY_FATAL);
			e.printStackTrace();
			return new ArrayList<UserEntity>();
		}
	}

	public void prepareList(){
		clear(true);
		users = new ArrayList<UserEntity>();
		users.addAll(getAllUsers());
	}	
	
	public void fillUserTypes(){
		if (userTypes == null || userTypes.isEmpty()){
			userTypes = new ArrayList<SelectItem>();
			userTypes.add(new SelectItem(UserEntity.COMMON,ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"user_common")));
			userTypes.add(new SelectItem(UserEntity.ADMIN,ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"user_admin")));
		}
	}
	
	public void approve(){
	
		try {
			userEntity = userEntityController.retrieveUserEntity(userEntity.getLogin());
			userEntity.setActive(true);
			userEntityController.updateUserEntity(userEntity);
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"user_approved"), FacesMessage.SEVERITY_INFO);
			userEntity = null;
			prepareList();
		} catch (DBContainerNotInitializedException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"database_error"), FacesMessage.SEVERITY_FATAL);
			return;
		} catch (EntityNotExistsException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"user_not_found"), FacesMessage.SEVERITY_ERROR);
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void deactivate(){
		
		try {
			userEntity = userEntityController.retrieveUserEntity(userEntity.getLogin());
			userEntity.setActive(false);
			userEntityController.updateUserEntity(userEntity);
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"user_deactived"), FacesMessage.SEVERITY_INFO);
			userEntity = null;
			prepareList();
		} catch (DBContainerNotInitializedException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"database_error"), FacesMessage.SEVERITY_FATAL);
			return;
		} catch (EntityNotExistsException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"user_not_found"), FacesMessage.SEVERITY_ERROR);
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void prepareUpdate(){
		clear(false);
		newEmail = userEntity.getEmail();
		fillUserTypes();
		userType = userEntity.getType();
	}
	
	public void update(){
		try {
			userEntity = userEntityController.retrieveUserEntity(userEntity.getLogin());
			userEntity.setEmail(newEmail);
			if (newPassword != null && !newPassword.isEmpty()){
				userEntity.setPass(userEntityController.generatePassword(newPassword));
			}
			userEntity.setType(userType);
			userEntityController.updateUserEntity(userEntity);
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"user_has_been_updated"), FacesMessage.SEVERITY_INFO);
			userEntity = null;
			RequestContext context = RequestContext.getCurrentInstance();
			context.addCallbackParam(ViewUtil.CALLBACK_CLOSE, true);
			prepareList();
		} catch (DBContainerNotInitializedException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"database_error"), FacesMessage.SEVERITY_FATAL);
			return;
		} catch (EntityNotExistsException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"user_not_found"), FacesMessage.SEVERITY_ERROR);
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void prepareDelete(){
		clear(false);
	}
	
	public void delete(){
		try {
			userEntityController.deleteUserEntity(userEntity);
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"user_has_been_deleted"), FacesMessage.SEVERITY_INFO);
			userEntity = null;
			prepareList();
		} catch (DBContainerNotInitializedException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"database_error"), FacesMessage.SEVERITY_FATAL);
			return;
		} catch (EntityNotExistsException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"user_not_found"), FacesMessage.SEVERITY_ERROR);
			return;
		}
	}
	
	
	public UserEntity getUserEntity() {
		return userEntity;
	}

	public void setUserEntity(UserEntity userEntity) {
		this.userEntity = userEntity;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public List<UserEntity> getUsers() {
		return users;
	}

	public void setUsers(List<UserEntity> users) {
		this.users = users;
	}

	public String getNewEmail() {
		return newEmail;
	}

	public void setNewEmail(String newEmail) {
		this.newEmail = newEmail;
	}

	public List<SelectItem> getUserTypes() {
		return userTypes;
	}

	public void setUserTypes(List<SelectItem> userTypes) {
		this.userTypes = userTypes;
	}

	public int getUserType() {
		return userType;
	}

	public void setUserType(int userType) {
		this.userType = userType;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

}
